package 剑指offer;

public class 剑指Offer11旋转数组的最小数字 {
    //暴力解法
//    public int minArray(int[] numbers) {
//        int min = numbers[0];
//        for(int i = 1;i < numbers.length;i++){
//            if(numbers[i] < min){
//                return numbers[i];
//            }
//        }
//        return min;
//    }
    //二分解法
    public static void main(String[] args) {
        int[] arr = new int[]{2,3,1};
        System.out.println(minArray(arr));
    }
    public static int minArray(int[] numbers) {
        return isSort(0,numbers.length-1,numbers);
    }
    //二分查找
    private static int isSort(int low ,int high ,int[] arr){
        int mid = (low + high)/2;
        //找中间值，
        if (arr[mid] < arr[high]){
            return isSort(low,mid,arr);
        }else if (arr[mid] > arr[high]){
            return isSort(mid+1,high,arr);
        }else {
            if (low == mid){
                return arr[low];
            }else {
                int i = isSort(low,mid,arr);
                int j = isSort(mid,high,arr);
                return i <= j ? i : j;
            }
        }

    }
}
